/*
 * @Author: 李一番
 * @Date: 2021-10-09 16:29:55
 * @Last Modified by: 李一番
 * @Last Modified time: 2021-12-28 15:34:43
 */

import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import type { App } from 'vue'

import Layout from '@/layout/index.vue';

export type RouteRecordRaws = RouteRecordRaw & {
  hidden?: boolean
}
const constantRoutes: RouteRecordRaws[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login/index.vue'),
    hidden: true
  },
  {
    path: '/user',
    component: Layout,
    redirect: '/user/info',
    hidden: true,
    children: [
      {
        path: 'info',
        component: () => import('@/views/User/info.vue'),
        name: 'UserInfo',
        meta: { title: 'userInfo', icon: 'el-icon-s-home' }
      }
    ]
  },
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
  }
]
const asyncRoutes: RouteRecordRaws[] = [
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        component: () => import('@/views/Dashboard/index.vue'),
        name: 'Dashboard',
        meta: { title: 'dashboard', icon: 'el-icon-s-home', affix: true },
      }
    ]
  },
  {
    path: '/error',
    name: 'ErrorPages',
    component: Layout,
    redirect: '/error/401',
    meta: { title: 'errorPages', icon: 'el-icon-s-home',  api: 'error_page'},
    children: [
      {
        path: '401',
        component: () => import('@/views/ErrorPage/401.vue'),
        name: 'Page401',
        meta: { title: 'page401', api: 'error_page_401' }
      },
      {
        path: '404',
        component: () => import('@/views/ErrorPage/404.vue'),
        name: 'Page404',
        meta: { title: 'page404', api: 'error_page_404' }
      }
    ]
  },
  { path: '/:pathMatch(.*)', redirect: '/error/404', hidden: true }
]

const router = createRouter({
  history: createWebHistory(),
  routes: constantRoutes.concat(asyncRoutes),
  strict: true,
  scrollBehavior: () => ({ left: 0, top: 0 })
})

export function setupRouter(app: App<Element>) {
  app.use(router)
}

export default router
